package com.hj.authority.controller;

import com.hj.authority.domain.dto.EmailSendDTO;
import com.hj.authority.domain.query.SysAccountLoginQuery;
import com.hj.authority.domain.vo.HjTokenVO;
import com.hj.authority.domain.vo.SysCaptchaVO;
import com.hj.authority.service.SysAuthService;
import com.hj.authority.service.SysCaptchaService;
import com.hj.response.constant.RespConstant;
import com.hj.security.utils.TokenUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author hj
 * @Date 2023/4/27
 * @Description: 认证管理
 */
@RestController
@RequestMapping("sys/auth")
@Tag(name = "认证管理")
@AllArgsConstructor
public class SysAuthController {

    private final SysCaptchaService sysCaptchaService;

    private final SysAuthService sysAuthService;

    @GetMapping("captcha")
    @Operation(summary = "captcha验证码")
    public SysCaptchaVO captcha() {
        return sysCaptchaService.generate();
    }


    @PostMapping("login")
    @Operation(summary = "账号密码登录")
    public HjTokenVO login(@RequestBody SysAccountLoginQuery query) {
        HjTokenVO token = sysAuthService.loginByAccount(query);
        System.out.println(token);
        return token;
    }


    @PostMapping("logout")
    @Operation(summary = "退出")
    public String logout(HttpServletRequest request) {
        sysAuthService.logout(TokenUtils.getAccessToken(request));
        return RespConstant.SUCCESS;
    }

    @PostMapping("email")
    @Operation(summary = "发送邮件验证码")
    public String email(EmailSendDTO dto) {
       return   sysAuthService.email(dto);
    }


}
